<template>

    <div class="tmpl">

        <div id="comment">
            <h3>提交评论</h3>
            <p class="line"></p>
            <textarea placeholder="请输入评论信息" v-model='postcontent'></textarea>
            <mt-button type="primary" size='large' @click='postcomment'>发表</mt-button>
        </div>

        <div id="list">
            <h3>评论列表</h3>
            <p class="line"></p>
            <div v-for="(item,index) in list">   
                <div class="title">
                    <span>第 {{index+1 }} 楼：</span>
                    <span>用户：{{item.user_name}}</span>
                    <span>发表时间：{{item.add_time | datefmt('YYYY-MM-DD HH:mm:ss') }} </span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell" v-text='item.content'></li>
                </ul>
            </div>
        </div>

        <mt-button type='danger' size='large' plain @click='getmore' v-text='loading'></mt-button>

    </div>
    
</template>

<script>
    import common from '../../kits/common.js';
    import { Toast } from 'mint-ui';
    export default{
        props:['id'],
        data(){
            return{
                postcontent:'',
                list:[],
                pageindex:1,
                loading:'加载更多'
            }
        },
        created(){
           this.getcomment(this.pageindex);
        },
        methods:{
            //点击提交
            postcomment(){
                if(this.postcontent.trim().length <= 0){
                    Toast('您要评论的内容不能为空');
                    return;
                }
                var url = common.apidomain+'/api/postcomment/'+this.id;
                this.$http.post(url,{content:this.postcontent},{emulateJSON:true}).then((res)=>{
                    Toast(res.body.message);
                    this.list = [{
                        'user_name':'CC',
                        'add_time':new Date(),
                        'content':this.postcontent
                    }].concat(this.list);
                    this.postcontent = ''; //提交成功清空
                    //this.pageindex = 1;
                    //this.getcomment(this.pageindex);
                })
            },
            getcomment(pageindex){
                pageindex = pageindex || 1;
                var url = common.apidomain+'/api/getcomments/'+this.id+'?pageindex='+pageindex;
                this.$http.get(url).then((res)=>{
                    if(res.body.status!=0){
                        Toast(res.body.message);
                        return;
                    }
                    if(res.body.message.length<=0){
                        this.loading = '无法加载更多评论';
                        this.pageindex--;
                        return;
                    }
                    // console.log(res.body.message);
                    this.list = this.list.concat(res.body.message);
                })
            },
            getmore(){
                this.pageindex++;  
                this.getcomment(this.pageindex);
            }
        }
    }
</script>

<style scoped>

    #comment,
    #list,
    .title{
        padding: 5px;
    }
    .title{
        color: #6d6d72;
        font-size: 15px;
        background-color: rgba(0, 0, 0, .1);
    }

</style>